package com.example.velhamesquita;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;

public class Contatos{

	public ArrayList<Contato> listar(Context context){
		ArrayList<Contato> lContatos = new ArrayList<Contato>();
		
		try {
			String[] PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER };
			 
			Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, PROJECTION, null, null, null);
			if (cursor.moveToFirst()) {
				String clsPhonename = null;
				String clsphoneNo = null;
				 
				do {
					clsPhonename = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
					clsphoneNo = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
					 
					clsphoneNo.replaceAll("\\D", "");
					clsPhonename = clsPhonename.replaceAll("&amp;", "");
					clsPhonename.replace("|", "");
					String clsPhoneName = clsPhonename.replace("|", "");
					 
					Contato c = new Contato();
					c.setNome(clsPhoneName);
					c.setFone(clsphoneNo);
					
					lContatos.add(c);
				} while (cursor.moveToNext());
			}
			return lContatos;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}